#! /usr/bin/env python2
import socket
import pygame
import pygtk
import gtk
import cv2.cv as cv


from Animated_Fish_V06 import VideoFishTank
from multiprocessing import Process, Queue
from audio import SendAudio, RecvAudio
from frame_serve import serve
from time import sleep

from frame_get import main

SCREENSIZE = [1024,768]


def serproc(add, queue):
	print "Sending video..."
	serve(add, queue)
	
def cliproc(add, queue):
	print "Receiving video..."
	pygame.init()
	VideoFishTank().main(add, queue)
	pygame.quit()
	#main(add)
	
def audsproc(add):
	print "Receiving audio..."
	start = RecvAudio(add)
	gtk.main()

def audcproc(add):
	print "Sending audio..."
	start = SendAudio(add)
	gtk.main()

if __name__ == "__main__":
	host = '192.168.10.101'
	host = '127.0.1.1'

	port = 20002
	localframeQ = Queue(1)

	# Serve video
	sendVideoProc = Process(target=serproc, args=[(host,port), localframeQ])
	sendVideoProc.start()
	# Prepare to receive audio
	#recvAudioProc = Process(target=audsproc, args=[(host,port+1)])
	#recvAudioProc.start()

	# Connecting to receive video or send audio before the remote
	# program is ready to send/accept will cause the processes to
	# crash. 
	sleep(4)
	
	rhost = '192.168.10.190'
#	rhost = '127.0.1.1'
	rport = port

	# Receive video
	recvVideoProc = Process(target=cliproc, args=[(rhost,rport), localframeQ])
	recvVideoProc.start()
	# Send audio
	#print "Here"
	#sendAudioProc = Process(target=audcproc, args=[(rhost,rport+1)])
	#sendAudioProc.start()
